package com.ccrfid.rmc6.appconfig.amq;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.SimpleJmsListenerContainerFactory;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.core.JmsTemplate;

import javax.jms.ConnectionFactory;

@Configuration
public class LocalActiveMQConfig {

    @Primary
    @Bean("connectionFactory")
    public ConnectionFactory connectionFactory(
            @Value("${local.activemq.broker-url}") String url,
            @Value("${local.activemq.user}") String username,
            @Value("${local.activemq.password}") String passwd) {
        ActiveMQConnectionFactory amqConnFactory = new ActiveMQConnectionFactory(username, passwd, url);
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(amqConnFactory);
        return connectionFactory;
    }

    @Bean("topicListenerCF")
    public SimpleJmsListenerContainerFactory topicListenerContainerFactory(
            @Qualifier("connectionFactory") ConnectionFactory connectionFactory) {
        SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setPubSubDomain(true);
        return factory;
    }
//    @Bean("topicListenerCF")
//    public DefaultJmsListenerContainerFactory topicListenerContainerFactory(
//            @Qualifier("connectionFactory") ConnectionFactory connectionFactory) {
//        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
//        factory.setConnectionFactory(connectionFactory);
//        factory.setPubSubDomain(true);
//        factory.setConcurrency("1-1");    //topic设置大于1时会重复接收消息
//        return factory;
//    }

    @Bean("jmsTopicTemplate")
    public JmsTemplate jmsTopicTemplate(@Qualifier("connectionFactory") ConnectionFactory connectionFactory) {
        JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
        jmsTemplate.setPubSubDomain(true);
        return jmsTemplate;
    }

    //    @Bean("queueListenerCF")
//    public SimpleJmsListenerContainerFactory queueListenerContainerFactory(
//            @Qualifier("connectionFactory") ConnectionFactory connectionFactory) {
//        SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
//        factory.setConnectionFactory(connectionFactory);
//        factory.setMessageConverter(new MappingJackson2MessageConverter());
//        return factory;
//    }
    @Bean("queueListenerCF")
    public DefaultJmsListenerContainerFactory queueListenerContainerFactory(
            @Qualifier("connectionFactory") ConnectionFactory connectionFactory) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setConcurrency("3-10");
        return factory;
    }

    @Bean("jmsQueueTemplate")
    public JmsTemplate jmsQueueTemplate(@Qualifier("connectionFactory") ConnectionFactory connectionFactory) {
        JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
        return jmsTemplate;
    }


}
